/*
 * Filename CheckProjectService.java 2022年10月20日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.terminal.service;

import com.ehome.poct.base.api.dto.device.DeviceClinicDTO;
import com.ehome.poct.base.api.feign.DeviceClient;
import com.ehome.poct.core.api.feign.CheckClient;
import com.ehome.poct.terminal.api.vo.CheckOrderProjectVO;
import com.ehome.poct.terminal.api.vo.CheckProjectGroupVO;
import com.ehome.poct.terminal.mapping.CheckAppMapping;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wyc
 * @since 2022/10/20 14:17
 **/
@Service
@AllArgsConstructor
public class CheckProjectService {
    private final CheckClient checkClient;
    private final DeviceClient deviceClient;
    private final CheckAppMapping checkAppMapping;

    public CheckOrderProjectVO getCheckProjectWithGroup(Long clinicId) {
        final var orderProject = new CheckOrderProjectVO();
        final var hasJby = deviceClient.getByClinicIdAndType(new DeviceClinicDTO().setClinicId(clinicId).setDeviceType(1)) != null;
        final var hasBld = deviceClient.getByClinicIdAndType(new DeviceClinicDTO().setClinicId(clinicId).setDeviceType(2)) != null;
        final var jbyProject = checkClient.getEhomeProjectWithType(1);
        final var bldProject = checkClient.getEhomeProjectWithType(2);
        final var uniProject = checkClient.getEhomeProjectWithType(3);
        // 不需要展示的检测项目
        final var displayList = List.of(1, 2, 3, 18, 21);
        // 常见检测项目
        final var oftenList = List.of(4, 5, 121);
        final var showJbyProject = jbyProject.stream()
                .filter(it -> !it.getType().equals(20))
                .filter(it -> !displayList.contains(it.getCode()))
                .map(checkAppMapping::to)
                .collect(Collectors.toList());
        final var oftenProjectDTO = jbyProject.stream()
                .filter(it -> oftenList.contains(it.getCode())).collect(Collectors.toList());
        oftenProjectDTO.addAll(uniProject);
        final var jbyGroup = new CheckProjectGroupVO("金标仪项目", showJbyProject);
        final var bldGroup = new CheckProjectGroupVO("血常规项目", checkAppMapping.to(bldProject));
        final var oftenGroup = new CheckProjectGroupVO("常见检测项目", checkAppMapping.to(oftenProjectDTO));
        orderProject.setProjects(List.of(oftenGroup, bldGroup, jbyGroup));
        if (hasJby && hasBld) {
            orderProject.setDeviceBind(3);
        } else if (hasJby) {
            orderProject.setDeviceBind(1);
        } else if (hasBld) {
            orderProject.setDeviceBind(2);
        }
        return orderProject;
    }
}